package com.dferreira.languagesteach.data_adapters;

import android.content.Context;
import android.view.View;

import com.dferreira.languagesteach.LanguagesTeachApplication;
import com.dferreira.languagesteach.activities.generic.IFragmentStack;
import com.dferreira.languagesteach.constants.Constants;
import com.dferreira.languagesteach.custom_items.UITableViewCell;
import com.dferreira.languagesteach.data_models.MetaSubject;
import com.dferreira.languagesteach.data_models.Lesson;
import com.dferreira.languagesteach.data_parsers.XMLLessonParser;
import com.dferreira.languagesteach.data_adapters.generic.NavigationAdapter;
import com.dferreira.languagesteach.fragments.WorkoutsFragment;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class SubjectsAdapter extends NavigationAdapter {


    /**
     * @param context       reference to the which the dataSource belongs
     * @param fragmentStack where the fragment will be putted
     */
    public SubjectsAdapter(Context context, IFragmentStack fragmentStack, String packageInstalledFolder) {
        super(context, packageInstalledFolder);
        setFragmentStack(fragmentStack);
    }

    /**
     */
    public boolean buildDataSet() {
        // Local variables
        Lesson lesson;

        lesson = parseLessonXML();

        if (lesson == null) {
            return false;
        }

        // Build from Subject Cells
        buildSubjectsList(lesson);
        dataSetWellConstructed();
        return true;
    }

    /**
     * Parse the lesson.xml file from some package
     *
     * @return the lesson structured as a data model lesson
     */
    private Lesson parseLessonXML() {
        // Local variables
        SAXParserFactory spf;
        SAXParser sp;
        XMLReader xr;
        XMLLessonParser xmlLessonParser;
        File file;
        InputStream inputStream;
        String lessonFolder;

        lessonFolder = LanguagesTeachApplication.getApplicationState().getLessonFolder();
        try {
            // parsing from XML file
            spf = SAXParserFactory.newInstance();
            sp = spf.newSAXParser();
            xr = sp.getXMLReader();
            xmlLessonParser = new XMLLessonParser();
            xr.setContentHandler(xmlLessonParser);
            file = new File(getPackageInstalledFolder()
                    + File.separator + lessonFolder
                    + File.separator + Constants.LESSON_FILE_NAME);
            inputStream = new FileInputStream(file);
            xr.parse(new InputSource(inputStream));
            inputStream.close();
            return xmlLessonParser.getLesson();
        } catch (FileNotFoundException e) {
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Build from Subject Cells
     *
     * @param lesson structure which supports language lesson
     */
    private void buildSubjectsList(Lesson lesson) {
        // Local variables
        Iterator<MetaSubject> metaSubjects;

        metaSubjects = lesson.getMetaSubjects().iterator();
        while (metaSubjects.hasNext()) {
            UITableViewCell tmpCell;
            MetaSubject metaSubject = metaSubjects.next();
            tmpCell = new UITableViewCell();
            tmpCell.setTextLabel(metaSubject.getTitle());
            tmpCell.setTag(metaSubject.getFolder());
            add(tmpCell);
        }
    }


    /**
     * Starts the workout activity
     */
    private void pushWorkoutFragment() {
        WorkoutsFragment workoutsFragment = new WorkoutsFragment();
        workoutsFragment.setArguments(getPackageInstalledFolder(),getFragmentStack());
        getFragmentStack().pushFragment(workoutsFragment);
    }

    /**
     * Handle a click from a level
     *
     * @param v view of the subject pressed
     */
    @Override
    public void onClick(View v) {
        String subjectTag;
        subjectTag = (String) v.getTag();
        LanguagesTeachApplication.getApplicationState().setSubject(subjectTag);

        pushWorkoutFragment();
    }


}
